﻿var bootstrap = function ($, learun) {
    "use strict";

    var page = {
        init: function () {
            page.bind();
            var date = new Date();
            $("#year").val(date.getFullYear());
            page.initData(date.getFullYear());
        },
        bind: function () {
            $('#files').lrUploader({isView: false});

            $("#year").change(function () {
                var year = $(this).val();
                page.initData(year);
            });

            $('#lr_save_btn').on('click', function () {
                if (!$('#form').lrValidform()) {
                    return false;
                }
                var formData = $('#form').lrGetFormData();

                learun.layerConfirm('注：预算总额设置后不允许再修改，您确认要提交吗？', function (res, index) {
                    if (res) {
                        $.lrSaveForm(config.base_server() + 'pbdbudgetinits/saveOrUpdate?access_token=' + config.getToken().access_token, JSON.stringify(formData), function (res) {
                            if (res.resp_code == 200) {
                                page.initData($("#year").val());
                            }
                        });
                        top.layer.close(index); //再执行关闭
                    }
                });

                return false;
            });
        },
        initData: function (year) {
            $.lrSetForm(config.base_server() + 'pbdbudgetinits/findByYear?year=' + year, function (data) {
                if (data) {
                    $('#form').lrSetFormData(data);
                    $("#totalMoney").attr("disabled","disabled");
                    $("#availableMoney").attr("disabled","disabled");

                    $("#totalMoney").css("background-color","#ddd");
                    $("#availableMoney").css("background-color","#ddd");
                } else {
                    $("#totalMoney").val('');
                    $("#availableMoney").val('');
                    $("#adjustMoney").val('');
                    $('#files').lrUploaderSet(learun.newGuid());
                    $("#totalMoney").removeAttr("disabled");
                    $("#availableMoney").removeAttr("disabled");
                    $("#totalMoney").css("background-color","#fff");
                    $("#availableMoney").css("background-color","#fff");
                }
            });
        }
    };
    page.init();
}